package com.eqs.enterprisequestionnaire.service;

import com.eqs.enterprisequestionnaire.model.pojo.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.eqs.enterprisequestionnaire.model.enums.QuestionEnum.isChoiceType;

@Service
public class DifferenceAnalysisService {
    @Resource
    private AnswerService answerService;
    @Resource
    private QuestionService questionService;
    @Resource
    private HistoryService historyService;
    @Resource
    private UserService userService;
    @Resource
    private ResponseService responseService;

    /*
    * 单个问题 性别差异化分析
    * */
    public Map<String,Map<Boolean,Integer>> getGenderDifference(Integer questionId){

        Question question = questionService.selectByQuestionId(questionId);

        // 判断是否为选择题类型
        if (!isChoiceType(question.getType())) {
            return null;
        }

        // 获取该问题的所有选项
        List<Option> options = historyService.getListOptionByQuestionId(questionId);
        //根据选项获得差异化分析Map
        Map<String,Map<Boolean,Integer>> result = new HashMap<>();

        for (Option option : options){
            //初始化选项差异化
            Map<Boolean,Integer> difference =new HashMap<>();

            //根据选项ID获取该选项的所有答案
            /*Answer answer =new Answer();
            answer.setContent((option.getId().toString()));
            List<Answer> answers = answerService.selectAll(answer);*/

            List<Answer> answers =answerService.selectAllAnswer(option.getId());

            for(Answer a :answers) {
                //该答案关联的用户
                User user = answerService.selectUser(a);

                difference.merge(user.getGender(), 1,
                        (v1,v2)->v1+1);
            }
            result.put(option.getContent(),difference);
        }
        return result;
    }

    /*
    * 单个问题 年龄差异化分析
    * */
    public Map<String,Map<String,Integer>> getAgeDifference(Integer questionId){

        Question question = questionService.selectByQuestionId(questionId);

        // 判断是否为选择题类型
        if (!isChoiceType(question.getType())) {
            return null;
        }

        // 获取该问题的所有选项
        List<Option> options = historyService.getListOptionByQuestionId(questionId);

        //根据选项获得差异化分析Map
        Map<String,Map<String,Integer>> result = new HashMap<>();
        //初始化？

        for (Option option : options){


            //初始化选项差异化
            Map<String,Integer> difference =new HashMap<>();
            // 初始化年龄阶段
            String[] ageStages = {"20-25", "25-30", "30-35","35-40","40-45","45-50","50+"};

            //根据选项ID获取该选项的所有答案
            List<Answer> answers =answerService.selectAllAnswer(option.getId());

            for(Answer a :answers) {
                //该答案关联的用户
                User user = answerService.selectUser(a);

                Integer age =userService.calculateUserAge(user.getId());
                String ageStage = null;
                if (age >= 20 && age <= 25) {
                    ageStage = "20-25";
                } else if (age > 25 && age <= 30) {
                    ageStage = "25-30";
                } else if (age > 30 && age <= 35) {
                    ageStage = "30-35";
                } else if (age > 35 && age <= 40) {
                    ageStage = "35-40";
                } else if (age > 40 && age <= 45) {
                    ageStage = "40-45";
                } else if (age > 45 && age <= 50) {
                    ageStage = "45-50";
                } else if (age > 50) {
                    ageStage = "50+";
                }

                difference.merge(ageStage, 1,
                        (v1,v2)->v1+1);
            }
            result.put(option.getContent(),difference);
        }
        return result;
    }

    /*
    * 单个问题 公司差异化分析
    * */
    public Map<String,Map<String,Integer>> getEnterpriseDifference(Integer questionId){

        Question question = questionService.selectByQuestionId(questionId);

        // 判断是否为选择题类型
        if (!isChoiceType(question.getType())) {
            return null;
        }

        // 获取该问题的所有选项
        List<Option> options = historyService.getListOptionByQuestionId(questionId);

        //根据选项获得差异化分析Map
        Map<String,Map<String,Integer>> result = new HashMap<>();

        for (Option option : options){

            //初始化选项差异化
            Map<String,Integer> difference =new HashMap<>();


            //根据选项ID获取该选项的所有答案
            List<Answer> answers =answerService.selectAllAnswer(option.getId());

            for(Answer a :answers) {
                //该答案关联的用户
                User user = answerService.selectUser(a);

                difference.merge(user.getEnterprise(), 1,
                        (v1,v2)->v1+1);

            }
            result.put(option.getContent(),difference);
        }
        return result;
    }

    /*
     * 单个问题 职位差异化分析
     * */
    public Map<String,Map<String,Integer>> getRoleDifference(Integer questionId){

        Question question = questionService.selectByQuestionId(questionId);

        // 判断是否为选择题类型
        if (!isChoiceType(question.getType())) {
            return null;
        }

        // 获取该问题的所有选项
        List<Option> options = historyService.getListOptionByQuestionId(questionId);

        //根据选项获得差异化分析Map
        Map<String,Map<String,Integer>> result = new HashMap<>();

        for (Option option : options){

            //初始化选项差异化
            Map<String,Integer> difference =new HashMap<>();


            //根据选项ID获取该选项的所有答案
            List<Answer> answers =answerService.selectAllAnswer(option.getId());

            for(Answer a :answers) {
                //该答案关联的用户
                User user = answerService.selectUser(a);

                difference.merge(user.getRoleName(), 1,
                        (v1,v2)->v1+1);
            }
            result.put(option.getContent(),difference);
        }
        return result;
    }

    /*
     * 整个问卷 性别差异化分析
     * */
    public Map<Boolean,Integer> getGenderDifferenceBig(Integer questionnaireId){

        Response response =new Response();
        response.setQuestionnaireId(questionnaireId);

        //根据选项获得差异化分析Map
        Map<Boolean,Integer> result = new HashMap<>();

        //获取该问卷的所有回答response
        List<Response> responses = responseService.selectAll(response);
        for (Response r : responses){
            User user=responseService.selectUser(r);

            result.merge(user.getGender(), 1,
                    (v1,v2)->v1+1);
        }
        return result;
    }

    /*
     * 整个问卷 年龄差异化分析
     * */
    public Map<String,Integer> getAgeDifferenceBig(Integer questionnaireId){

        Response response =new Response();
        response.setQuestionnaireId(questionnaireId);

        //根据选项获得差异化分析Map
        Map<String,Integer> result = new HashMap<>();

        //获取该问卷的所有回答response
        List<Response> responses = responseService.selectAll(response);
        for (Response r : responses){
            User user=responseService.selectUser(r);

            Integer age =userService.calculateUserAge(user.getId());
            String ageStage = null;
            if (age >= 20 && age <= 25) {
                ageStage = "20-25";
            } else if (age > 25 && age <= 30) {
                ageStage = "25-30";
            } else if (age > 30 && age <= 35) {
                ageStage = "30-35";
            } else if (age > 35 && age <= 40) {
                ageStage = "35-40";
            } else if (age > 40 && age <= 45) {
                ageStage = "40-45";
            } else if (age > 45 && age <= 50) {
                ageStage = "45-50";
            } else if (age > 50) {
                ageStage = "50+";
            }
            result.merge(ageStage, 1,
                    (v1,v2)->v1+1);

        }
        return result;
    }
    /*
     * 整个问卷 公司差异化分析
     * */
    public Map<String,Integer> getEnterpriseDifferenceBig(Integer questionnaireId){

        Response response =new Response();
        response.setQuestionnaireId(questionnaireId);

        //根据选项获得差异化分析Map
        Map<String,Integer> result = new HashMap<>();

        //获取该问卷的所有回答response
        List<Response> responses = responseService.selectAll(response);
        for (Response r : responses){
            User user=responseService.selectUser(r);

            result.merge(user.getEnterprise(), 1,
                    (v1,v2)->v1+1);

        }
        return result;
    }
    /*
     * 整个问卷 职位差异化分析
     * */
    public Map<String,Integer> getRoleDifferenceBig(Integer questionnaireId){

        Response response =new Response();
        response.setQuestionnaireId(questionnaireId);

        //根据选项获得差异化分析Map
        Map<String,Integer> result = new HashMap<>();

        //获取该问卷的所有回答response
        List<Response> responses = responseService.selectAll(response);
        for (Response r : responses){
            User user=responseService.selectUser(r);

            result.merge(user.getRoleName(), 1,
                    (v1,v2)->v1+1);
        }
        return result;
    }
}
